/*
 * Copyright 2010 Daniel Kurka
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package org.bsc.mgwt.client.dialog;

import org.bsc.mgwt.client.widget.base.HasLabel;

import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.event.dom.client.BlurEvent;
import com.google.gwt.event.dom.client.BlurHandler;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.dom.client.FocusEvent;
import com.google.gwt.event.dom.client.FocusHandler;
import com.google.gwt.event.dom.client.HasChangeHandlers;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.event.shared.GwtEvent;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.i18n.client.HasDirection.Direction;
import com.google.gwt.user.client.ui.HasValue;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;
import com.googlecode.mgwt.ui.client.MGWT;
import com.googlecode.mgwt.ui.client.dialog.Dialog;
import com.googlecode.mgwt.ui.client.theme.base.DialogCss;
import com.googlecode.mgwt.ui.client.theme.base.InputCss;
import com.googlecode.mgwt.ui.client.widget.base.HasAutoCapitalize;
import com.googlecode.mgwt.ui.client.widget.base.HasAutoCorrect;
import com.googlecode.mgwt.ui.client.widget.base.HasPlaceHolder;
import com.googlecode.mgwt.ui.client.widget.touch.TouchPanel;

/**
 * 
 * @author softphone
 *
 */
public class InputDialog extends BaseInputDialog implements HasLabel, HasAutoCapitalize, HasAutoCorrect, HasPlaceHolder, HasValue<String>, HasChangeHandlers, Dialog {
	
	private Label 		textLabel = new Label(); 
	private Label 		errorLabel = new Label();
	private TextBox 	box = new TextBox();
	

	public InputDialog(DialogCss css, InputCss inputCss, String title,	InputCallback<InputDialog> _callback) {
		super(css, inputCss, _callback);
		
		setTitleText(title);
		initContentPanel();
	}

	public InputDialog(String title, InputCallback<InputDialog> callback) {
		super(callback);

		setTitleText(title);
		initContentPanel();
		
	}

	protected void initContentPanel() {

		errorLabel.setHorizontalAlignment( Label.ALIGN_CENTER);	
		errorLabel.getElement().getStyle().setColor("red");
		errorLabel.getElement().getStyle().setPaddingTop(5, Unit.PX);
		errorLabel.getElement().getStyle().setFontSize(16, Unit.PX);

		box.addBlurHandler(new BlurHandler() {

			@Override
			public void onBlur(BlurEvent event) {
				MGWT.fixIOSScrollIssueBlur();

			}
		});

		box.addFocusHandler(new FocusHandler() {

			@Override
			public void onFocus(FocusEvent event) {
				errorLabel.setText( "" );
				MGWT.fixIOSScrollIssueFocus();

			}
		});
		
		box.addStyleName(inputCss.box());
		
		TouchPanel main = new TouchPanel();
		
		main.addStyleName( inputCss.textBox() );
		main.add( textLabel );
		main.add(box);
		main.add( errorLabel );
		
		getContent().add(main);
	}

	public String getErrorLabel() {
		return errorLabel.getText();
	}

	public void setErrorLabel(String text) {
		errorLabel.setText(text);

	}

	public void setErrorLabel(String value, Direction dir) {
		errorLabel.setText(value, dir);
	}
	
	@Override
	public String getLabel() {
		return textLabel.getText();
	}

	@Override
	public void setLabel(String text) {
		textLabel.setText(text);

	}

	@Override
	public void setLabel(String value, Direction dir) {
		textLabel.setText(value, dir);
	}

	@Override
	public void setLabelStyle(boolean horizontal) {
	}

	@Override
	public void setPlaceHolder(String value) {
		box.getElement().setAttribute("placeholder", value);
	}

	@Override
	public String getPlaceHolder() {
		return box.getElement().getAttribute("placeholder");
	}

	public void show() {
		errorLabel.setText( "" );
		super.show();
	}

	@Override
	public HandlerRegistration addValueChangeHandler(ValueChangeHandler<String> handler) {
		return box.addValueChangeHandler(handler);
	}
	@Override
	public HandlerRegistration addChangeHandler(ChangeHandler handler) {
		return box.addChangeHandler(handler);
	}

	@Override
	public void fireEvent(GwtEvent<?> event) {
		box.fireEvent(event);
		
	}

	@Override
	public String getValue() {
		return box.getValue();
	}

	@Override
	public void setValue(String value) {
		box.setValue(value);
	}

	@Override
	public void setValue(String value, boolean fireEvents) {
		box.setValue(value, fireEvents);
		
	}

	@Override
	public void setAutoCorrectEnabled(boolean enabled) {
		if (enabled) {
			box.getElement().setPropertyString("autocorrect", "on");
		} else {
			box.getElement().setPropertyString("autocorrect", "off");
		}

	}

	@Override
	public boolean isAutoCorrectEnabled() {
		String autoCorrent = box.getElement().getPropertyString("autocorrect");
		return "on".equals(autoCorrent);
	}

	@Override
	public void setAutoCapitalize(boolean capitalize) {
		if (capitalize) {
			box.getElement().setPropertyString("autocapitalize", "on");
		} else {
			box.getElement().setPropertyString("autocapitalize", "off");
		}

	}

	@Override
	public boolean isAutoCapitalize() {
		String auto = box.getElement().getPropertyString("autocapitalize");
		return "on".equals(auto);
	}



}
